3. 无重复字符的最长子串
为保证权益,题目请参考 3. 无重复字符的最长子串(From LeetCode).
解决方案1
CPP
C++
// 3
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int dp[128];
fill(dp, dp + 128, -1);
int ans = 0; // result
int left_index = -1; // the left index of the s(array).
for (int i = 0; i < s.length(); ++i) {
int tmp = dp[int(s[i])];
if (tmp > left_index) {
left_index = tmp;
}
ans = max(ans, i - left_index);
dp[int(s[i])] = i;
}
return ans;
}
};
int main() {
Solution so;
cout << so.lengthOfLongestSubstring(" ") << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33